package com.hwadee.property.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwadee.property.mapper.BuildingMapper;
import com.hwadee.property.pojo.Building;
import com.hwadee.property.service.IBuildingService;
import com.hwadee.property.dto.PageResult;
import com.hwadee.property.util.BusinessException;
import com.hwadee.property.util.ErrorCodes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements IBuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public PageResult<Building> getPage(int current, int size, Long communityId, Long buildingNumber,
                                        Integer buildingUnit, Integer floorCount, Integer households,
                                        String location, String houseType) {
        // 确保 current 至少为 1
        current = Math.max(current, 1);
        size = Math.max(size, 1);

        // 计算偏移量
        int offset = (current - 1) * size;

        // 查询总记录数
        int total = buildingMapper.countBuildings(communityId, buildingNumber, buildingUnit, floorCount, households, location, houseType);

        // 只有当 total > 0 时才执行查询，减少数据库负载
        List<Building> records = (total > 0) ? buildingMapper.getBuildings(communityId, buildingNumber, buildingUnit, floorCount, households, location, houseType, size, offset) : Collections.emptyList();

        return new PageResult<>(total, current, size, records);
    }

    @Override
    public List<Building> listByCommunityId(Long communityId) {
        if (communityId == null) {
            throw new BusinessException(ErrorCodes.INVALID_INPUT, "小区ID不能为空");
        }
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", communityId);
        return this.list(queryWrapper);
    }

    @Override
    public Building getByBuildingNumber(Long communityId, Long buildingNumber) {
        if (communityId == null || buildingNumber == null) {
            throw new BusinessException(ErrorCodes.INVALID_INPUT, "小区ID和楼栋编号不能为空");
        }
        QueryWrapper<Building> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", communityId)
                .eq("building_number", buildingNumber);
        return this.getOne(queryWrapper);
    }

    @Override
    public boolean addBuilding(Building building) {
        if (building == null || building.getCommunityId() == null || building.getBuildingNumber() == null) {
            throw new BusinessException(ErrorCodes.INVALID_INPUT, "楼栋信息不能为空");
        }

        // 校验楼栋编号是否重复
        Building existing = this.getByBuildingNumber(building.getCommunityId(), building.getBuildingNumber());
        if (existing != null) {
            throw new BusinessException(ErrorCodes.ALREADY_EXISTS, "楼栋编号在该小区中已存在！");
        }
        return this.save(building);
    }

    @Override
    public boolean updateBuilding(Building building) {
        if (building == null || building.getId() == null) {
            throw new BusinessException(ErrorCodes.INVALID_INPUT, "楼栋ID不能为空");
        }

        // 校验楼栋是否存在
        if (this.getById(building.getId()) == null) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "楼栋信息不存在！");
        }
        return this.updateById(building);
    }

    @Override
    public boolean deleteBuilding(Long id) {
        if (id == null) {
            throw new BusinessException(ErrorCodes.INVALID_INPUT, "楼栋ID不能为空");
        }

        // 校验楼栋是否存在
        if (this.getById(id) == null) {
            throw new BusinessException(ErrorCodes.NOT_FOUND, "楼栋信息不存在！");
        }
        return this.removeById(id);
    }
}
